import subprocess
import json
def test_speed():
    # 使用subprocess模块运行speedtest-cli命令
    result = subprocess.run(['speedtest-cli', '--json'], capture_output=True, text=True)

    # 解析JSON输出
    speed_test_result = json.loads(result.stdout)
    
    # 提取下载速度和上传速度（单位：Mbit/s）
    download_speed = speed_test_result['download'] / 1024 / 1024
    upload_speed = speed_test_result['upload'] / 1024 / 1024

    print(f"下载速度: {download_speed:.2f} Mbit/s")
    print(f"上传速度: {upload_speed:.2f} Mbit/s")

# 运行速度测试
test_speed()